﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CreatPipeInsulation : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			Transaction transaction = null;
			FormCreatPipeInsulation formCreatPipeInsulation = new FormCreatPipeInsulation();
			Result result;
			try
			{
				Document document = commandData.Application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Selection selection = activeUIDocument.Selection;
				if (CreatPipeInsulation.hWndRevit == null)
				{
					CreatPipeInsulation.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				formCreatPipeInsulation.Show(CreatPipeInsulation.hWndRevit);
				int wMsg = 7;
				CreatPipeInsulation.SendMessage(CreatPipeInsulation.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				double mediumTemp = 0.0;
				double designTemp = 0.0;
				double dewingTemp = 0.0;
				InsulationLayerParam insulationLayerParam = null;
				List<Pipe> list = new List<Pipe>();
				PipeInsulationManage pipeInsulationManage = new PipeInsulationManage(activeUIDocument);
				transaction = new Transaction(document, "创建保温层类型");
				transaction.Start();
				pipeInsulationManage.CreatInsulationType();
				transaction.Commit();
				for (;;)
				{
					string pipeSystem = pipeInsulationManage.GetPipeSystem();
					if (string.IsNullOrEmpty(pipeSystem))
					{
						break;
					}
					for (;;)
					{
						try
						{
							list = pipeInsulationManage.GetCalcInsulationPipes(pipeSystem);
							if (list == null || list.Count < 1)
							{
								continue;
							}
							pipeInsulationManage.GetInterfaceSetting(ref mediumTemp, ref designTemp, ref dewingTemp, ref insulationLayerParam, formCreatPipeInsulation);
							PipeInsulationCalc pipeInsulationCalc = new PipeInsulationCalc(insulationLayerParam, mediumTemp, designTemp, dewingTemp);
							int calcTypeNum = formCreatPipeInsulation.GetCalcTypeNum();
							Dictionary<Pipe, double> dictionary = pipeInsulationCalc.CalcThickness(list, calcTypeNum);
							if (calcTypeNum == 1 || calcTypeNum == 2)
							{
								formCreatPipeInsulation.hyTxtCalcDepth.Text = dictionary.First<KeyValuePair<Pipe, double>>().Value.ToString();
							}
							else
							{
								dictionary = new Dictionary<Pipe, double>();
								double defineThickness = formCreatPipeInsulation.GetDefineThickness();
								foreach (Pipe key in list)
								{
									dictionary.Add(key, defineThickness);
								}
							}
							transaction = new Transaction(document, "添加管道保温层");
							transaction.Start();
							pipeInsulationManage.AddPipesInsulationLayer(pipeInsulationManage.GetInsulationTypeByName(insulationLayerParam.Name), dictionary);
							transaction.Commit();
							continue;
						}
						catch (Exception ex)
						{
							string message2 = ex.Message;
							break;
						}
						goto IL_1EE;
					}
				}
				IL_1EE:
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				formCreatPipeInsulation.Close();
				if (transaction != null && (int)(int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
